package com.xyht.medicine_management_system.modules.addresses.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.addresses.entity.MmsAddresses;
import com.xyht.medicine_management_system.modules.addresses.entity.req.MmsAddressesReq;
import com.xyht.medicine_management_system.modules.addresses.mapper.MmsAddressesMapper;
import com.xyht.medicine_management_system.modules.addresses.service.MmsAddressesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.addresses.entity.resp.MmsAddressesResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 地址表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-03
 */
@Service
public class MmsAddressesServiceImpl extends ServiceImpl<MmsAddressesMapper, MmsAddresses> implements MmsAddressesService {
    @Resource
    private MmsAddressesMapper mmsAddressesMapper;

    @Override
    public ResponseResult addAddresses(MmsAddressesReq addressesReq, String userId) {
        if (isNullOrEmpty(addressesReq.getReceiver(), addressesReq.getPhone(), addressesReq.getProvince(), addressesReq.getCity(),
                addressesReq.getArea(), addressesReq.getAddressDetail(), addressesReq.getIsDefault())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (addressesReq.getIsDefault()) {
            List<MmsAddresses> defaultAddressList = mmsAddressesMapper.selectList(new LambdaQueryWrapper<MmsAddresses>().eq(MmsAddresses::getIsDefault, addressesReq.getIsDefault()));
            //若存在默认地址则 将历史默认地址取消默认
            if (!defaultAddressList.isEmpty()) {
                MmsAddresses defaultAddress = defaultAddressList.get(0);
                defaultAddress.setIsDefault(false);
                mmsAddressesMapper.updateById(defaultAddress);
            }
        }

        //添加本地址
        MmsAddresses addresses = new MmsAddresses();
        BeanUtils.copyProperties(addressesReq, addresses);
        addresses.setUserId(userId);
        mmsAddressesMapper.insert(addresses);

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateAddresses(MmsAddressesReq addressesReq, String userId) {
        if (isNullOrEmpty(addressesReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsAddresses addresses = mmsAddressesMapper.selectById(addressesReq.getId());
        if (isNullOrEmpty(addresses)) {
            return CommonResult.failed(CommonCodeEnum.ADDRESS_NOT_EXIST);
        }

        if (addressesReq.getIsDefault()) {
            List<MmsAddresses> defaultAddressList = mmsAddressesMapper.selectList(new LambdaQueryWrapper<MmsAddresses>().eq(MmsAddresses::getIsDefault, addressesReq.getIsDefault()));
            //若存在默认地址则 将历史默认地址取消默认
            if (!defaultAddressList.isEmpty()) {
                MmsAddresses defaultAddress = defaultAddressList.get(0);
                defaultAddress.setIsDefault(false);
                mmsAddressesMapper.updateById(defaultAddress);
            }
        }

        MmsAddresses updAddress = new MmsAddresses();
        BeanUtils.copyProperties(addressesReq, updAddress);
        updAddress.setId(addresses.getId());
        updAddress.setUserId(userId);

        mmsAddressesMapper.updateById(updAddress);

        return CommonResult.success();
    }

    @Override
    public ResponseResult listAddresses(Integer pageNum, Integer pageSize, String searchStr, String userId) {
        LambdaQueryWrapper<MmsAddresses> queryWrapper = new LambdaQueryWrapper<MmsAddresses>()
                .eq(MmsAddresses::getUserId, userId)
                .orderByDesc(MmsAddresses::getCreateTime);

        if (!isNullOrEmpty(searchStr)) {
            StringUtils.deleteWhitespace(searchStr);
            queryWrapper.like(MmsAddresses::getReceiver, searchStr).or()
                    .like(MmsAddresses::getPhone, searchStr).or()
                    .like(MmsAddresses::getProvince, searchStr).or()
                    .like(MmsAddresses::getCity, searchStr).or()
                    .like(MmsAddresses::getArea, searchStr).or()
                    .like(MmsAddresses::getVillage, searchStr).or()
                    .like(MmsAddresses::getAddressDetail, searchStr);
        }

        Page<MmsAddresses> page = new Page<>(pageNum, pageSize);
        mmsAddressesMapper.selectPage(page, queryWrapper);

        List<MmsAddressesResp> addressesRespList = page.getRecords().stream().map(addresses -> {
            MmsAddressesResp addressesResp = new MmsAddressesResp();
            BeanUtils.copyProperties(addresses, addressesResp);
            return addressesResp;
        }).collect(Collectors.toList());

        return CommonResult.success(addressesRespList, (int) page.getTotal());
    }

    @Override
    public ResponseResult addressesDetail(String addressId) {
        if (isNullOrEmpty(addressId)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsAddresses addresses = mmsAddressesMapper.selectById(addressId);
        if (isNullOrEmpty(addresses)) {
            return CommonResult.failed(CommonCodeEnum.ADDRESS_NOT_EXIST);
        }

        MmsAddressesResp addressesResp = new MmsAddressesResp();
        BeanUtils.copyProperties(addresses, addressesResp);

        return CommonResult.success(addressesResp);
    }
}
